const express = require('express')
const router = express.Router()
const result = require('../db/result')

router.get('/getComment/:websiteId',(req,res)=>{
    const websiteId=req.params.websiteId
    console.log(websiteId)
    let sql=`SELECT c.commentId,c.websiteId,c.parentId,c.userId,u.nickname,u.avatar,c.content,c.creatTime FROM comment AS c,user AS u WHERE c.userId=u.userId and c.websiteId= ${websiteId}`
    result(sql,res)
})

router.get('/getComment/:id',(req,res)=>{
    // console.log(req)
    const id=req.params.id
    let sql=`select * from comment where id=${id}`
    console.log(sql)
    result(sql,res)
})

router.post('/saveComment',(req,res)=>{
    // console.log(req.body.reply_content)
    const name=req.body.from
    const reply_content=req.body.comment
    // const parent_id=req.body.parent_id
    // const create_time=new Date()
    let sql=`insert into comment (name, content) values ('${name}','${reply_content}')`
    result(sql,res)
})

router.delete('/delComment/:id',(req,res)=>{
    const id=req.params.id
    let sql=`delete from comment where id= ${id}`
    result(sql,res)
})

module.exports=router